/*
 * @Author: Zhou Zijian
 * @Date: 2023-03-06 01:05:55
 * @Last Modified by: Zhou Zijian
 * @Last Modified time: 2023-03-06 02:24:35
 */

#include <iostream>
#include <memory>
#include "GpuX.h"

int main()
{
    int ret = GpuX::ErrorCode::SUCCESS;
    ret = GpuX::GetInstance().Create("../../data/myjson.json");
    if (ret != GpuX::ErrorCode::SUCCESS) {
        std::cout << __LINE__ << " : " << ret << std::endl;
        return ret;
    }
    constexpr int size = 10;
    std::unique_ptr<float[]> a(new float[size]);
    std::unique_ptr<float[]> b(new float[size]);
    std::unique_ptr<float[]> result(new float[size]);
    for (int i = 0; i < size; i++) {
        a[i] = static_cast<float>(i);
        b[i] = static_cast<float>(i);
        result[i] = 0.0f;
    }
    auto bufA = GpuX::GetInstance().CreateBuffer(a.get(), size * sizeof(float));
    auto bufB = GpuX::GetInstance().CreateBuffer(b.get(), size * sizeof(float));
    auto bufResult = GpuX::GetInstance().CreateBuffer(result.get(), size * sizeof(float));
    ret = GpuX::GetInstance().Run("sub", true, bufA->GetClMem(), bufB->GetClMem(), bufResult->GetClMem());
    if (ret != GpuX::ErrorCode::SUCCESS) {
        std::cout << __LINE__ << " : " << ret << std::endl;
        return ret;
    }
    ret = bufA->Flush();
    if (ret != GpuX::ErrorCode::SUCCESS) {
        std::cout << __LINE__ << " : " << ret << std::endl;
        return ret;
    }
    ret = bufB->Flush();
    if (ret != GpuX::ErrorCode::SUCCESS) {
        std::cout << __LINE__ << " : " << ret << std::endl;
        return ret;
    }
    ret = bufResult->Flush();
    if (ret != GpuX::ErrorCode::SUCCESS) {
        std::cout << __LINE__ << " : " << ret << std::endl;
        return ret;
    }
    for (int i = 0; i < size; i++) {
        std::cout << result[i] << " ";
    }
    std::cout << std::endl;
    GpuX::GetInstance().DestroyBuffer(bufResult);
    GpuX::GetInstance().DestroyBuffer(bufB);
    GpuX::GetInstance().DestroyBuffer(bufA);
    return 0;
}
